package com.google.code.mamule.config;

import com.google.common.collect.ImmutableMap;

import org.h2.jdbcx.JdbcDataSource;
import org.h2.tools.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.SQLException;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * Created with IntelliJ IDEA. User: borck_000 ; Date: 12/21/13 ; Time: 9:40 PM
 */
@Configuration
@EnableJpaRepositories("com.google.code.mamule")
@EnableTransactionManagement
public class MamulePersistence {

  // doubles as DataSource
  @Bean(destroyMethod = "shutdown")
  public EmbeddedDatabase dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    builder.setType(EmbeddedDatabaseType.H2).setName("mamule-db");
    return builder.build();
  }
  
  @Bean
  public DataSource directDataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("org.h2.Driver");
      dataSource.setUrl("jdbc:h2:~/mamuledb");
      dataSource.setUsername("sa");
      dataSource.setPassword("");
      return dataSource;
  }


  // allows peeking into the db
  @Bean(initMethod = "start", destroyMethod = "stop")
  public Server h2WebServer() throws SQLException {
    return Server.createWebServer();
  }

  private static final Map<String, ?> JPA_PROPS =
      ImmutableMap.of("hibernate.ejb.naming_strategy",
                      "org.hibernate.cfg.DefaultComponentSafeNamingStrategy");

  @Bean
  public EntityManagerFactory entityManagerFactory() {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.google.code.mamule");
    factory.setJpaPropertyMap(JPA_PROPS);
    //factory.setDataSource(dataSource());
    factory.setDataSource(directDataSource());
    factory.afterPropertiesSet();

    return factory.getObject();
  }

  @Bean
  public PlatformTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory());
    return txManager;
  }
}
